<?php
/**
 * 用户佣金提现
 * User: ws
 * Date: 2018/10/11
 * Time: 14:11
 */

namespace app\admin\controller;
use think\Db;
class With  extends Init
{
    function index(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';
            if(is_numeric(input('type')) === true){
                $where['state'] = input('type');
            }
            $where['u.name'] = array('like','%'.trim($key).'%');
            $list = Db::table('je_withdrawal')->alias('w')->join('je_user u','w.user_id = u.user_id')->field('u.name,u.time,w.add_time,w.money,w.poundage,w.state,w.withdrawal_id')->order('withdrawal_id desc')->where($where)->paginate(input('limit'))->toArray();

            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }
        return view('list');
    }

    function edit(){
        $id = input('id');
        if(empty($id)){
            return json($result = ['code'=>0,'msg'=>'缺少参数']);
        }

        $info = Db::table('je_withdrawal')->alias('w')->join('je_user u','w.user_id = u.user_id')->field('u.name,u.time,u.phone,w.*')->where("w.withdrawal_id = {$id}")->find();
        $info['s_name'] = '';
        if(!empty($info['admin_user_id'])){$info['s_name'] = Db::name('admin_user')->where("admin_user_id={$info['admin_user_id']}")->value('name');}


        $log = Db::name('withdrawal_log')->where("withdrawal_id={$id}")->select();
        foreach ($log as $key=>$val){
            if($val['type']==0){
                $user = Db::name('user')->where("user_id={$val['u_id']}")->value('name');
                 $log[$key]['name'] = '用户：'.$user;
            }elseif($val['type']==1){
                $user = Db::table('je_admin_user')->alias('a')->join('je_admin_role r','a.admin_role_id = a.admin_role_id')->field('a.name,r.role_name')->where("a.admin_user_id = {$val['u_id']}")->find();
                $log[$key]['name'] = '平台--'.$user['role_name'].'：'.$user['name'];
            }elseif($val['type']==2){
                $log[$key]['name'] = '系统自动';
            }
        }

        $info['log'] = $log;

        return view('edit',['list'=>$info]);
    }


    function examine(){
        if(request()->isPost()){
            $data = $this->request->post();
            $data['admin_user_id'] =  $_SESSION['think']['admin_user']['admin_user_id'];
            $data['audit_time'] = time();

            $data2['u_id'] =  $_SESSION['think']['admin_user']['admin_user_id'];
            $data2['add_time'] = time();
            $data2['type'] = 1;
            //$data2['content'] = '审核失败，失败原因：'.$data['results'];
            $data2['withdrawal_id'] = $data['withdrawal_id'];

            if($data['withdrawal_type']==1){
                unset($data['withdrawal_type']);
            }

            if($data['state']==2){
                Db::name('withdrawal')->update($data);
                $data2['content'] = '审核失败，失败原因：'.$data['results'];
                $ru = Db::name('withdrawal_log')->insert($data2);
            }else if($data['state']==1 || $data['withdrawal_type']==2){
                if($data['withdrawal_type']!=2){
                    $data2['content'] = '审核成功';
                    $ru = Db::name('withdrawal_log')->insert($data2);
                }

                //提现
                $withdrawal= Db::name('withdrawal')->where("withdrawal_id={$data['withdrawal_id']}")->field('money,poundage,user_id')->find();
                $prce = $withdrawal['money']-$withdrawal['poundage'];
                $openid = Db::name('user')->where("user_id={$withdrawal['user_id']}")->value('openid');
                $pay = & load_wechat('Pay');
                $out_refund_no = 'T'.generateOrderNum();
                $result = $pay->transfers($openid, $prce*100, $out_refund_no,'佣金提现');

                if($result==false){
                    if($data['withdrawal_type']!=2) {
                        $data['pay_results'] = '支付失败，原因不明';
                        $data['sn'] = $out_refund_no;
                        $data['state'] = 4;
                        Db::name('withdrawal')->update($data);
                    }
                    $data2['type'] = 2;
                    $data2['content'] = '支付失败。支付渠道，微信支付；支付金额，￥'.$prce;

                    Db::name('withdrawal_log')->insert($data2);
                    return json(array('code'=>0,'msg'=>'操作失败，支付失败，原因不明'));
                }
                if($data['withdrawal_type']!=2) {
                    $data['pay_results'] = '支付成功，支付金额：￥' . $prce;
                    $data['sn'] = $out_refund_no;
                    $data['pay_time'] = $result['payment_time'];
                    $data['state'] = 3;
                    $data['pay_serial_number'] = $result['payment_no'];
                    Db::name('withdrawal')->update($data);
                }else{
                    $data_['pay_results'] = '支付成功，支付金额：￥' . $prce;
                    $data_['sn'] = $out_refund_no;
                    $data_['pay_time'] = $result['payment_time'];
                    $data_['state'] = 3;
                    $data_['pay_serial_number'] = $result['payment_no'];
                    $data_['withdrawal_id'] = $data['withdrawal_id'];
                    Db::name('withdrawal')->update($data_);
                }
                $data2['type'] = 2;
                $data2['content'] = '支付成功。支付渠道，微信支付；支付金额，￥'.$prce;

                //Db::name('user')->where('openid', $openid)->setDec('distribut_money',$withdrawal['money']);

                Db::name('withdrawal_log')->insert($data2);
                return json(array('code'=>200,'msg'=>'操作成功，支付成功'));
            }

            if (!$ru) {
                return json(array('code'=>0,'msg'=>'审核失败'));
            }
            return json(array('code'=>200,'msg'=>'审核成功'));
        }
    }
}